package com.linked.gateway.controller;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.linked.gateway.config.GatewayConfig;
import com.linked.gateway.config.GatewaySwitchConfig;
import com.linked.gateway.config.IPAccessControlConfig;
import com.linked.universal.common.Result;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

/**
 * @author :dbq
 * @date : 2022/9/14 10:00
 */
@RestController
@RequestMapping("config")
public class ConfigController {

    private final GatewayConfig gatewayConfig;

    private final IPAccessControlConfig ipConfig;

    private final GatewaySwitchConfig switchConfig;

    private final ObjectMapper mapper;

    private final static Logger LOGGER = LoggerFactory.getLogger(ConfigController.class);

    @Autowired
    public ConfigController(GatewayConfig gatewayConfig, IPAccessControlConfig ipConfig, GatewaySwitchConfig switchConfig, ObjectMapper mapper) {
        this.gatewayConfig = gatewayConfig;
        this.ipConfig = ipConfig;
        this.switchConfig = switchConfig;
        this.mapper = mapper;
    }



    @GetMapping("/gatewayConfig")
    public Result gatewayConfig() throws Exception {
        Map<String, Object> configMap = new HashMap();
        configMap.put("namespace", gatewayConfig.getNamespace());
        configMap.put("group", gatewayConfig.getGroup());
        configMap.put("dataId", gatewayConfig.getDataId());
        configMap.put("server-addr", gatewayConfig.getServiceAddr());
        return Result.success(configMap);
    }

    @GetMapping("/ipAccessControlConfig")
    public Result ipAccessControlConfig() throws Exception {
        Map<String, Object> configMap = new HashMap();
        configMap.put("config", mapper.writeValueAsString(ipConfig.getIpAccessControls()));
        return Result.success(configMap);
    }

    @GetMapping("/switchConfig")
    public Result switchConfig() throws Exception {
        Map<String, Object> configMap = new HashMap();
        configMap.put("config", switchConfig.toString());
        return Result.success(configMap);
    }
}
